﻿package com.moonshineproject.utils {
	import com.moonshineproject.IDEModel;
	import com.moonshineproject.view.MainView;
	import flash.desktop.Clipboard;
	import flash.desktop.ClipboardFormats;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import mx.core.IToolTip;
	import mx.core.UIComponent;
	import mx.managers.ToolTipManager;
	import no.doomsday.console.ConsoleUtil;
	public class Pastebin extends EventDispatcher {
		private static const PASTEBIN_API:String = "http://pastebin.com/api_public.php";
		
		private static var instances:Dictionary = new Dictionary();
		
		private var loader:URLLoader;
		
		private var tooltipPos:Point;
		private var tooltip:IToolTip;
		private var cleanupTimer:Timer;
		public function Pastebin(text:String, title:String = "", priv:Boolean = true, format:String = "text", subdomain:String = "", expire:String = "", email:String = "") {
			instances[this] = true;
			
			var req:URLRequest = new URLRequest(PASTEBIN_API);
			req.method = URLRequestMethod.POST;
			
			var vars:URLVariables = new URLVariables();
			vars["paste_code"] = text;
			vars["paste_private"] = priv ? 1 : 0;
			vars["paste_format"] = format;
		
			if (title != "") vars["paste_name"] = title;
			if (subdomain != "") vars["paste_subdomain"] = subdomain;
			if (expire != "") vars["paste_expire_date"] = expire; 
			if (email != "") vars["paste_email"] = email;
			
			req.data = vars;
			
			var mv:MainView = IDEModel.getInstance().mainView;
			tooltip = ToolTipManager.createToolTip("Pasting...", 0, 0);
			tooltipPos = new Point(mv.stage.mouseX, mv.stage.mouseY);
			moveTooltip();
			
			loader = new URLLoader(req);
			loader.addEventListener(Event.COMPLETE, complete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, error);
		}
		private function moveTooltipLater():void {
			(tooltip as UIComponent).callLater(moveTooltip);
		}
		private function moveTooltip():void {
			tooltip.move(tooltipPos.x-tooltip.measuredWidth/2, tooltipPos.y-tooltip.measuredHeight-10);
		}
		
		
		private function cleanup():void {
			loader = null;
			delete instances[this];
			
			cleanupTimer = new Timer(2000, 1);
			cleanupTimer.addEventListener(TimerEvent.TIMER_COMPLETE, cleanupDelayed);
			cleanupTimer.start();
		}
		private function cleanupDelayed(e:TimerEvent):void {
			cleanupTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, cleanupDelayed);
			cleanupTimer = null;
			
			ToolTipManager.destroyToolTip(tooltip);
		}
		
		private function complete(e:Event):void {
			var response:String = String(loader.data);
			var match:Array = response.match(/ERROR: (.*)/);
			if (match) {
				dispatchEvent(new PastebinEvent(PastebinEvent.ERROR, match[1]));
				tooltip.text = "Pastebin error";
			} else {
				Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, response);
				dispatchEvent(new PastebinEvent(PastebinEvent.SUCCESS, response));
				tooltip.text = "Link copied to clipboard";
			}
			moveTooltipLater();
			cleanup();
		}
		private function error(e:IOErrorEvent):void {
			dispatchEvent(new PastebinEvent(PastebinEvent.ERROR, ""+e));
			tooltip.text = "Pastebin connection error";
			moveTooltipLater();
			cleanup();
		}
	}
}